(논문) STUDY1
Cat/Dog_accuracy
전체 이미지를 대상으로 $\theta$를 바꿔가며 accuracy 변화를 확인하자
import torch
from fastai.vision.all import *
import cv2
path=untar_data(URLs.PETS)/'images'
path
files=get_image_files(path)
def label_func(f):
if f[0].isupper():
return 'cat'
else:
return 'dog'
dls=ImageDataLoaders.from_name_func(path,files,label_func,item_tfms=Resize(512))
lrnr=cnn_learner(dls,resnet34,metrics=error_rate)
lrnr.fine_tune(1)
net1=lrnr.model[0]
net2=lrnr.model[1]
net2 = torch.nn.Sequential(
torch.nn.AdaptiveAvgPool2d(output_size=1),
torch.nn.Flatten(),
torch.nn.Linear(512,out_features=2,bias=False))
net=torch.nn.Sequential(net1,net2)
lrnr2=Learner(dls,net,metrics=accuracy)
lrnr2.fine_tune(15)
interp = ClassificationInterpretation.from_learner(lrnr2)
interp.plot_confusion_matrix()
interp = Interpretation.from_learner(lrnr2)
interp.plot_top_losses(16)
files
fig, ax = plt.subplots(5,5)
k=0
for i in range(5):
for j in range(5):
x, = first(dls.test_dl([PILImage.create(get_image_files(path)[k])]))
camimg = torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x).squeeze())
a,b = net(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
test=camimg[0]-torch.min(camimg[0])
A1=torch.exp(-0.1*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show(ax=ax[i][j])
ax[i][j].set_title("cat(%s)" % catprob.round(5))
else:
test=camimg[1]-torch.min(camimg[1])
A1=torch.exp(-0.1*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show(ax=ax[i][j])
ax[i][j].set_title("dog(%s)" % dogprob.round(5))
k=k+1
fig.set_figwidth(16)
fig.set_figheight(16)
fig.tight_layout()
img = PILImage.create(get_image_files(path)[3])
img
x, = first(dls.test_dl([img]))
fig, (ax1,ax2,ax3) = plt.subplots(1,3)
#
dls.train.decode((x,))[0].squeeze().show(ax=ax1)
ax1.set_title("Input image")
#
dls.train.decode((x,))[0].squeeze().show(ax=ax2)
ax2.imshow(camimg[0].to("cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax2.set_title("CAT PART")
#
dls.train.decode((x,))[0].squeeze().show(ax=ax3)
ax3.imshow(camimg[1].to("cpu").detach(),alpha=0.5,extent=(0,511,511,0),interpolation='bilinear',cmap='cool')
ax3.set_title("DOG PART")
#
fig.set_figwidth(12)
fig.set_figheight(12)
fig.tight_layout()
fig, ax = plt.subplots(5,5)
k=0
for i in range(5):
for j in range(5):
x, = first(dls.test_dl([PILImage.create(get_image_files(path)[k])]))
camimg = torch.einsum('ij,jkl -> ikl', net2[2].weight, net1(x).squeeze())
a,b = net(x).tolist()[0]
catprob, dogprob = np.exp(a)/ (np.exp(a)+np.exp(b)) , np.exp(b)/ (np.exp(a)+np.exp(b))
if catprob>dogprob:
test=camimg[0]-torch.min(camimg[0])
A1=torch.exp(-0.015*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show(ax=ax[i][j])
ax[i][j].set_title("cat(%s)" % catprob.round(5))
else:
test=camimg[1]-torch.min(camimg[1])
A1=torch.exp(-0.015*test)
X1=np.array(A1.to("cpu").detach(),dtype=np.float32)
Y1=torch.Tensor(cv2.resize(X1,(512,512),interpolation=cv2.INTER_LINEAR))
x1=x.squeeze().to('cpu')*Y1-torch.min(x.squeeze().to('cpu')*Y1)
(x1*0.35).squeeze().show(ax=ax[i][j])
ax[i][j].set_title("dog(%s)" % dogprob.round(5))
k=k+1
fig.set_figwidth(16)
fig.set_figheight(16)
fig.tight_layout()